# Maintainer: Ronald van Haren <ronald.archlinux.org>
# Maintainer: Bruno Pagani <archange@archlinux.org>
# Contributor: damir <damir@archlinux.org>

_pkg=netcdf
_mpi=openmpi
pkgname=${_pkg}-${_mpi}
pkgver=4.9.2
pkgrel=3
pkgdesc="network Common Data Form interface for array-oriented data access and corresponding library with parallel support (${_mpi} version)"
arch=(loong64 x86_64)
url="https://www.unidata.ucar.edu/software/netcdf/"
license=(custom)
depends=("hdf5-${_mpi}" "pnetcdf-${_mpi}" curl libxml2 libzip)
makedepends=(cmake)
optdepends=('netcdf-fortran: fortran bindings' 'netcdf-cxx: c++ bindings')
checkdepends=(inetutils unzip)
provides=("${_pkg}")
conflicts=("${_pkg}")
options=(!makeflags)
source=(https://github.com/Unidata/netcdf-c/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz)
sha256sums=('bc104d101278c68b303359b3dc4192f81592ae8640f1aee486921138f7f88cb7')
prepare() {
  # https://github.com/Unidata/netcdf-c/issues/2188 https://github.com/Unidata/netcdf-c/issues/2242
  sed -i "/tst_remote3/d" ${_pkg}-c-${pkgver}/ncdap_test/CMakeLists.txt
}

build() {
  export CC=mpicc
  cmake -B build -S ${_pkg}-c-${pkgver} \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_BYTERANGE=ON \
    -DENABLE_CDF5=ON \
    -DENABLE_PNETCDF=ON \
    -DENABLE_DAP_LONG_TESTS=ON \
    -DENABLE_DAP_REMOTE_TESTS=ON \
    -DENABLE_EXAMPLE_TESTS=ON \
    -DENABLE_EXTRA_TESTS=ON \
    -DENABLE_FILTER_TESTING=ON \
    -DENABLE_LARGE_FILE_TESTS=ON \
    -DENABLE_UNIT_TESTS=ON
  make -C build
}


check() {
  # This is required starting with OpenMPI 3.0 when trying to run more
  # processes than the number of available cores
  export OMPI_MCA_rmaps_base_oversubscribe=yes
  # We don’t have CUDA by default
  export OMPI_MCA_opal_warn_on_missing_libcuda=0

  make -C build test
}

package() {
  make -C build DESTDIR="${pkgdir}" install
  install -Dm644 ${_pkg}-c-${pkgver}/COPYRIGHT -t "${pkgdir}"/usr/share/licenses/${_pkg}/
}
